#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
快速依赖检查脚本

检查所有转换工具的依赖是否已安装，并提供安装建议。
"""

import sys
import subprocess
from pathlib import Path

# 设置路径
sys.path.insert(0, str(Path(__file__).parent))

def check_package(package_name, import_name=None):
    """检查Python包是否可用"""
    import_name = import_name or package_name
    try:
        __import__(import_name)
        return True, "✅ 已安装"
    except ImportError:
        return False, "❌ 未安装"

def check_system_command(command):
    """检查系统命令是否可用"""
    try:
        result = subprocess.run([command, '--version'], 
                              capture_output=True, text=True, timeout=5)
        if result.returncode == 0:
            return True, "✅ 已安装"
        else:
            return False, "❌ 未安装"
    except (subprocess.TimeoutExpired, FileNotFoundError):
        return False, "❌ 未安装"

def main():
    """主函数"""
    print("="*60)
    print("依赖检查报告")
    print("="*60)
    
    # PDF转换依赖
    print("\n📄 PDF转换:")
    marker_available, marker_status = check_package("marker", "marker")
    print(f"  marker: {marker_status}")
    if not marker_available:
        print("    安装: pip install marker-pdf")
    
    docling_available, docling_status = check_package("docling")
    print(f"  docling: {docling_status}")
    if not docling_available:
        print("    安装: pip install docling")
    
    mineru_available, mineru_status = check_package("mineru")
    print(f"  mineru: {mineru_status}")
    if not mineru_available:
        print("    安装: 需要特殊安装，请先安装uv，然后运行 uv pip install -U \"mineru[core]\"")
    
    # DOCX/DOC转换依赖
    print("\n📝 DOCX/DOC转换:")
    python_docx_available, python_docx_status = check_package("python-docx", "docx")
    print(f"  python-docx: {python_docx_status}")
    if not python_docx_available:
        print("    安装: pip install python-docx")
    
    print(f"  docling: {docling_status}")
    
    pandoc_available, pandoc_status = check_system_command("pandoc")
    print(f"  pandoc: {pandoc_status}")
    if not pandoc_available:
        print("    安装: https://pandoc.org/installing.html")
    
    libreoffice_available, libreoffice_status = check_system_command("libreoffice")
    print(f"  libreoffice: {libreoffice_status}")
    if not libreoffice_available:
        print("    安装: sudo apt-get install libreoffice (Ubuntu/Debian)")
        print("          brew install --cask libreoffice (macOS)")
    
    # PPT转换依赖
    print("\n🎯 PPT转换:")
    pptx2md_available, pptx2md_status = check_package("pptx2md")
    print(f"  pptx2md: {pptx2md_status}")
    if not pptx2md_available:
        print("    安装: pip install pptx2md")
    
    print(f"  docling: {docling_status}")
    
    # 图片OCR依赖
    print("\n🖼️  图片OCR:")
    paddleocr_available, paddleocr_status = check_package("paddleocr")
    print(f"  paddleocr: {paddleocr_status}")
    if not paddleocr_available:
        print("    安装: pip install paddleocr")
    
    easyocr_available, easyocr_status = check_package("easyocr")
    print(f"  easyocr: {easyocr_status}")
    if not easyocr_available:
        print("    安装: pip install easyocr")
    
    tesseract_available, tesseract_status = check_system_command("tesseract")
    print(f"  tesseract: {tesseract_status}")
    if not tesseract_available:
        print("    安装: sudo apt-get install tesseract-ocr (Ubuntu/Debian)")
        print("          brew install tesseract (macOS)")
    
    pytesseract_available, pytesseract_status = check_package("pytesseract")
    print(f"  pytesseract: {pytesseract_status}")
    if not pytesseract_available:
        print("    安装: pip install pytesseract")
    
    # OBS上传依赖
    print("\n☁️  OBS上传:")
    obs_available, obs_status = check_package("obs", "obs")
    print(f"  obs-sdk: {obs_status}")
    if not obs_available:
        print("    安装: pip install esdk-obs-python")
    
    dotenv_available, dotenv_status = check_package("python-dotenv", "dotenv")
    print(f"  python-dotenv: {dotenv_status}")
    if not dotenv_available:
        print("    安装: pip install python-dotenv")
    
    # 总结
    print("\n"+"="*60)
    print("总结")
    print("="*60)
    
    # 计算可用的转换器
    pdf_methods = sum([marker_available, docling_available, mineru_available])
    docx_methods = sum([docling_available, python_docx_available, pandoc_available])
    doc_methods = sum([libreoffice_available and docling_available, 
                      libreoffice_available and pandoc_available])
    ppt_methods = sum([pptx2md_available, docling_available])
    ocr_methods = sum([paddleocr_available, easyocr_available, 
                      tesseract_available and pytesseract_available])
    
    print(f"PDF转换方法: {pdf_methods}/3 可用")
    print(f"DOCX转换方法: {docx_methods}/3 可用")
    print(f"DOC转换方法: {doc_methods}/2 可用")
    print(f"PPT转换方法: {ppt_methods}/2 可用")
    print(f"OCR转换方法: {ocr_methods}/3 可用")
    print(f"OBS上传: {'可用' if obs_available else '不可用'}")
    
    total_available = pdf_methods + docx_methods + doc_methods + ppt_methods + ocr_methods
    total_possible = 13  # 总共13种方法
    
    print(f"\n整体可用性: {total_available}/{total_possible} ({total_available/total_possible*100:.1f}%)")
    
    if total_available < total_possible:
        print("\n💡 建议安装缺失的依赖以获得完整功能。")

if __name__ == "__main__":
    main() 